package com.example.homework.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;

import com.example.homework.R;
import com.example.homework.bean.TodoItem;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

public class TodoAdapter extends RecyclerView.Adapter<TodoAdapter.ViewHolder> {
    private final List<TodoItem> todoList;
    private final OnItemClickListener listener;

    // 定义回调接口
    public interface OnItemClickListener {
        void onEditClick(TodoItem item);        // 编辑按钮点击
        void onDeleteClick(TodoItem item);      // 删除按钮点击
        void onItemClick(TodoItem item);        // 列表项点击（标记完成）
    }

    // 构造函数
    public TodoAdapter(List<TodoItem> todoList, OnItemClickListener listener) {
        this.todoList = todoList;
        this.listener = listener;
    }

    // 创建 ViewHolder
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_todo, parent, false);
        return new ViewHolder(view);
    }

    // 绑定数据到 ViewHolder
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TodoItem item = todoList.get(position);

        // 设置文本内容
        holder.titleTextView.setText(item.getTitle());
        holder.descriptionTextView.setText(item.getDescription());
        holder.reminderTextView.setText(formatReminderTime(item.getReminderTime()));

        // 设置完成状态（通过 CheckBox 或列表项点击）
        holder.itemView.setSelected(item.isCompleted()); // 可结合背景选择器实现视觉反馈

        // 绑定点击事件
        holder.itemView.setOnClickListener(v -> listener.onItemClick(item));
        holder.editButton.setOnClickListener(v -> listener.onEditClick(item));
        holder.deleteButton.setOnClickListener(v -> listener.onDeleteClick(item));
    }

    // 返回列表项数量
    @Override
    public int getItemCount() {
        return todoList.size();
    }

    // 内部 ViewHolder 类
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final View itemView;
        public final TextView titleTextView;
        public final TextView descriptionTextView;
        public final TextView reminderTextView;
        public final ImageButton editButton;
        public final ImageButton deleteButton;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            this.itemView = itemView;
            titleTextView = itemView.findViewById(R.id.textViewTitle);
            descriptionTextView = itemView.findViewById(R.id.textViewDescription);
            reminderTextView = itemView.findViewById(R.id.textViewReminder);
            editButton = itemView.findViewById(R.id.buttonEdit);
            deleteButton = itemView.findViewById(R.id.buttonDelete);
        }
    }

    // 时间格式化方法（将毫秒转换为 "yyyy-MM-dd HH:mm" 格式）
    private String formatReminderTime(long timeInMillis) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        return format.format(timeInMillis);
    }
}
